package com.example.scaffolding.ui.loader

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.work.*
import com.example.scaffolding.databinding.FragmentLoaderBinding
import com.example.scaffolding.util.NetworkUtils
import com.example.scaffolding.util.EventListener
import com.example.scaffolding.composer.ImageComposer
import java.lang.Exception
import java.util.*
import com.example.scaffolding.R

class ImageLoadFragment : Fragment() {
    private lateinit var binding: FragmentLoaderBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentLoaderBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.button.setOnClickListener {
            if (!NetworkUtils.isNetworkConnected(context)) {
                Toast.makeText(context, "无网络连接",Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            val imageComposer = ImageComposer()
            imageComposer.addInput(getString(R.string.url_top), object : EventListener<String> {
                override fun onProgress(progress: Int) {
                    view.post {
//                        Log.d("Image", "$progress")
                        if(progress != 0) {
                            binding.textTopProgress.text = progress.toString() }
                        }
                }

                override fun onSuccess(t: String) {
                }

                override fun onError(e: Exception) {
                }

            })

            imageComposer.addInput(getString(R.string.url_bottom), object : EventListener<String> {
                override fun onProgress(progress: Int) {
                    view.post {
                        if(progress != 0) {
                            binding.textBottomProgress.text = progress.toString()
                        }
                    }
                }

                override fun onSuccess(t: String) {
                }

                override fun onError(e: Exception) {
                }

            })

            imageComposer.start(view.context, viewLifecycleOwner, binding.imageTop, binding.imageBottom)



//            HttpDownloader.download(binding.editTop.text.toString(), object : EventListener<String> {
//
//                override suspend fun onProgress(progress: Int) {
//                    Log.d(Companion.TAG, "onProgress: $progress")
//                }
//
//                override fun onSuccess(t: String) {
//                    Log.d(TAG, "onSuccess: ")
//                }
//
//                override fun onError(e: Exception) {
//                    Log.d(TAG, "onError: ")
//                }
//
//            })

        }

    }

    companion object {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private const val ARG_SECTION_NUMBER = "section_number"

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        @JvmStatic
        fun newInstance(sectionNumber: Int): ImageLoadFragment {
            return ImageLoadFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_SECTION_NUMBER, sectionNumber)
                }
            }
        }

        private const val TAG = "ImageLoadFragment"
    }
}